﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;


public static class algorithmMax
{
    #region 双色球 默认数据

    private static List<List<int>> two_color_ballData;

    public static List<List<int>> ShuangSeQiuData
    {
        get
        {
            if (two_color_ballData == null || two_color_ballData.Count == 0)
            {
                two_color_ballData = new List<List<int>>();
                two_color_ballData = reloadShuangseQiu();
            }

            return two_color_ballData;
        }
    }

    public static List<List<int>> reloadShuangseQiu()
    {
        List<List<int>> datas = new List<List<int>>();
        int max = 33;
        for (int i = 1; i <= max - 5; i++)
        {
            for (int i2 = i + 1; i2 <= max - 4; i2++)
            {
                for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                {
                    for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                    {
                        for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                        {
                            for (int i6 = i5 + 1; i6 <= max; i6++)
                            {
                                datas.Add(new List<int>() {i, i2, i3, i4, i5, i6});
                                //                                stream.WriteLine($"{i} , {i2} , {i3} , {i4} , {i5} , {i6}");
                            }
                        }
                    }
                }
            }
        }

        return datas;
    }

    #endregion

    #region 线程属性

    public static int ThreadMaxCount = 3;
    public static int ioThreadCount = 3;
    public static ShaStatues isRunStatus = ShaStatues.Idle;

    public static DateTime start_time;
    private static List<List<int>> thread_two_color_ballData;

    private static object locks;

    #endregion

    #region 事件回调

    public static event Action<string, ShaHaoType> onShaHaoChange;
    //public static event Action onShaHaoChange;

    #endregion

    public static void Init()
    {
        ThreadPool.SetMaxThreads(ThreadMaxCount, ioThreadCount);
        cur_two_color_ballData = ShuangSeQiuData;
        locks = new object();
    }

    private static List<List<int>> cur_two_color_ballData;


    public static void ShaMax(List<List<int>> input_nums)
    {
        StartDuplexSha(input_nums, cur_two_color_ballData);
    }


    public static void ShaSingNote(List<int> nums)
    {
        //StartDuplexSha(input_nums, cur_two_color_ballData);
    }

    public static List<List<int>> temp_data = new List<List<int>>();


    /// <summary>
    /// 杀多个大复试
    /// </summary>
    /// <param name="mutifyDuplexs"></param>
    public static void StartDuplexSha(List<List<int>> input_nums_array, List<List<int>> all_data)
    {
        start_time = DateTime.Now;
        var datas = new List<List<int>>();

        #region 大复式组合成单注

        for (int j = 0; j < input_nums_array.Count; j++)
        {
            List<int> input_nums = input_nums_array[j];
            var max = input_nums.Count;
            for (int i = 1; i <= max - 5; i++)
            {
                for (int i2 = i + 1; i2 <= max - 4; i2++)
                {
                    for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                    {
                        for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                        {
                            for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                            {
                                for (int i6 = i5 + 1; i6 <= max; i6++)
                                {
                                    datas.Add(new List<int>()
                                    {
                                        input_nums[i - 1], input_nums[i2 - 1], input_nums[i3 - 1],
                                        input_nums[i4 - 1],
                                        input_nums[i5 - 1],
                                        input_nums[i6 - 1]
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        #endregion


        Dictionary<int, List<List<int>>> values = new Dictionary<int, List<List<int>>>();
        var cc_count = Mathf.CeilToInt(all_data.Count / ThreadMaxCount);

        // 分配每个线程要杀的号
        for (int i = 0; i < ThreadMaxCount; i++)
        {
            List<List<int>> test = new List<List<int>>();
            var end_max = cc_count * (i + 1);
            if (end_max >= all_data.Count)
            {
                end_max = all_data.Count;
            }

            for (int j = i * cc_count; j < end_max; j++)
            {
                test.Add(all_data[j]);
            }

            values.Add(i, test);
        }

        temp_data = datas;

        if (_threadDatas == null)
        {
            _threadDatas = new List<ThreadData>();
        }

        _threadDatas.Clear();
        isRunStatus = ShaStatues.Running;
        for (int i = 0; i < ThreadMaxCount; i++)
        {
            string name = string.Format("ThreadPool_{0}", i);
            //            WaitCallback method = (t) => StartSha(datas,values[i]);
            List<List<int>> curdata = values[i];
            WaitCallback method = (t) =>
            {
                Debug.Log(datas.Count);

                StartSha(t as ThreadData);
            };

            _threadDatas.Add(new ThreadData() {name = name, curdata = curdata, input_data = datas});
            ThreadPool.QueueUserWorkItem(method, _threadDatas[i]);
        }
    }

    public static string curTips = "";

    public static void ChangeThreadStatus(ShaStatues status)
    {
        isRunStatus = status;
    }

    /// <summary>
    /// 开始杀
    /// </summary>
    private static void StartSha(ThreadData allData)
    {
        var start_count = allData.curdata.Count;
        for (int i = 0; i < allData.input_data.Count; i++)
        {
            var temp_data = allData.input_data[i];
            var index = allData.curdata.FindIndex((data) =>
            {
                // 6个数的检查
                for (int j = 0; j < data.Count; j++)
                {
                    if (data[j] != temp_data[j])
                    {
                        return false;
                    }
                }

                return true;
            });
            if (index != -1)
            {
                allData.curdata.RemoveAt(index);
            }

            curTips = $"线程{allData.name} 进度 {allData.curdata.Count}  ";
        }

        allData.isSuccess = true;
        Debug.Log($"{allData.name} 完成杀号");
        lock (locks)
        {
            for (int i = 0; i < _threadDatas.Count; i++)
            {
                if (_threadDatas[i].isSuccess == false)
                {
                    return;
                }
            }

            isRunStatus = ShaStatues.EndSha;
        }
    }


    public static List<ThreadData> _threadDatas;
}

public class ThreadData
{
    public string name;
    public List<List<int>> curdata;
    public List<List<int>> input_data;
    public bool isSuccess = false;
    public int Index;
    public ShaHaoType shaHaoType;

    public Action<int> end_action;
}